<template>
  <div class="page">
    <div class="navbar">
      <div class="navbar-inner sliding">
        <div class="left">
          <a href="#" class="link back">
            <i class="icon icon-back"></i>
            <span class="ios-only">Back</span>
          </a>
        </div>
        <div class="title navbar-calendar-title"></div>
      </div>
    </div>
    <div class="page-content">
      <div id="calendar" class="block block-strong no-padding no-margin no-hairline-top"></div>
      <div id="calendar-events" class="list no-margin no-hairlines no-ios-left-edge">
        <ul></ul>
      </div>
    </div>
  </div>
</template>
<style>
  #calendar, #calendar-events {
    height: 50%;
    box-sizing: border-box;
  }
  #calendar .calendar {
    height: 100%;
  }
  #calendar-events ul {
    height: 100%;
    overflow: auto;
  }
  @media (orientation: landscape) {
    #calendar {
      float: left;
    }
    #calendar, #calendar-events {
      height: 100%;
    }
    #calendar, #calendar-events {
      width: 50%;
    }
    #calendar-events {
      margin-left: 50% !important;
      border-left: 1px solid #eee;
    }
    .theme-dark #calendar-events {
      border-left-color: #282828;
    }
  }
</style>
<script>
  return {
    data: function () {
      var date = new Date();
      var year = date.getFullYear();
      var month = date.getMonth();
      var day = date.getDate();
      return {
        today: new Date(year, month, day),
        events: [
          {
            date: new Date(year, month, day, 12, 30),
            title: 'Meeting with Vladimir',
          },
          {
            date: new Date(year, month, day, 18, 00),
            title: 'Shopping',
          },
          {
            date: new Date(year, month, day, 21, 00),
            title: 'Gym',
          },
          {
            date: new Date(year, month, day + 2, 16, 00),
            title: 'Pay loan',
          },
          {
            date: new Date(year, month, day + 2, 21, 00),
            title: 'Gym',
          },
        ],
      }
    },
    methods: {
      renderEvents: function (calendar) {
        var self = this;
        var currentDate = calendar.value[0];
        var currentEvents = self.events
          .filter(function (event) {
            return (
              event.date.getTime() >= currentDate.getTime() &&
              event.date.getTime() < currentDate.getTime() + 24 * 60 * 60 * 1000
            );
          });
        var eventsHtml = '';
        if (currentEvents.length) {
          eventsHtml = currentEvents
            .map(function (event) {
              var hours = event.date.getHours();
              var minutes = event.date.getMinutes();
              if (minutes < 10) minutes = '0' + minutes;
              return '<li class="item-content">' +
                      '<div class="item-inner">' +
                        '<div class="item-title">' + (event.title) + '</div>' +
                        '<div class="item-after">' + (hours + ':' + minutes) + '</div>' +
                      '</div>' +
                    '</li>';
            })
            .join('');
        } else {
          eventsHtml = '<li class="item-content">' +
                        '<div class="item-inner">' +
                          '<div class="item-title text-color-gray">No events for this day</div>' +
                        '</div>' +
                      '</li>';
        }

        self.$el.find('.list ul').html(eventsHtml);
      },
    },
    on: {
      pageInit: function (e, page) {
        var self = this;
        var app = self.$app;
        var $ = self.$;
        var monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August' , 'September' , 'October', 'November', 'December'];
        self.calendar = app.calendar.create({
          containerEl: '#calendar',
          toolbar: false,
          value: [self.today],
          events: self.events.map(function (event) {
            return new Date(event.date.getFullYear(), event.date.getMonth(), event.date.getDate());
          }),
          on: {
            init: function (calendar) {
              $('.navbar-calendar-title').text(monthNames[calendar.currentMonth] +', ' + calendar.currentYear);
              app.navbar.size(page.navbarEl);
              calendar.$el.addClass('no-ios-right-edge');
              self.renderEvents(calendar);
            },
            monthYearChangeStart: function (calendar) {
              $('.navbar-calendar-title').text(monthNames[calendar.currentMonth] +', ' + calendar.currentYear);
              app.navbar.size(page.navbarEl);
            },
            change: function (calendar) {
              self.renderEvents(calendar);
            },
          }
        });
      },
      pageBeforeRemove() {
        var self = this;
        self.calendar.destroy();
      },
    },
  }
</script>

